<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;
use Leonis\Notifications\EasySms\Channels\EasySmsChannel;
use Leonis\Notifications\EasySms\Messages\EasySmsMessage;
use Overtrue\EasySms\PhoneNumber;

class SmsCode extends Notification
{
    use Queueable;

    public $code;

    public function __construct()
    {
        $this->code = rand(111111, 999999);
    }

    public function via($notifiable)
    {
        return [EasySmsChannel::class];
    }

    public function toEasySms($notifiable)
    {
        /** @var PhoneNumber $phoneNumber */
        $phoneNumber = Arr::first($notifiable->routes);
        Cache::put('sms_code:'.$phoneNumber->getUniversalNumber(), $this->code, now()->addMinutes(15));

        return (new EasySmsMessage())
            ->setContent('【OBG】您的验证码是'.$this->code.'。如非本人操作，请忽略本短信');
    }
}
